package com.intelligent.mts.client.core.view;

import com.intelligent.mts.client.core.controller.MainController;
import com.intelligent.mts.client.event.StatusBarChangeEvent;
import com.intelligent.mts.client.utils.*;
import com.intelligent.mts.common.utils.ApplicationException;
import com.intelligent.mts.common.utils.ExceptionMessages;
import com.intelligent.mts.common.utils.StringUtils;
import com.intelligent.mts.core.dto.CustomerDto;
import com.intelligent.mts.core.dto.FinishGoodDto;
import com.intelligent.mts.core.dto.ResultDto;
import com.intelligent.mts.core.entity.FinishGood;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import javax.annotation.PostConstruct;
import javax.swing.*;
import javax.swing.event.PopupMenuEvent;
import javax.swing.event.PopupMenuListener;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
import java.util.*;

/**
 * Created by Garen.Pang on 2016/8/23.
 */
@Component
public class BomSearchPanel extends JPanel{

    @Autowired
    MainController mainController;

    @PostConstruct
    public void initComponent(){

        lbcustomer = new JLabel();
        lbFGPN = new JLabel();
        customer = new JComboBox();
        FGPN = new JComboBox();

        btnSearch = new JButton();

        lbcustomer.setText("Customer: ");
        lbFGPN.setText("FG PN");
        btnSearch.setText("Search");
        btnSearch.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {

                btnSearchActionPerformed(e);
            }
        });
        customer.addItemListener(new ItemListener() {
            @Override
            public void itemStateChanged(ItemEvent e) {
                comboxChangeActionPerformed(e);
            }
        });

        FGPN.addItemListener(new ItemListener() {
            @Override
            public void itemStateChanged(ItemEvent e) {
                fgpncomboxChangeActionPerformed(e);
            }
        });

        this.setLayout(new GridBagLayout());

        this.add(lbcustomer, new GBC(0, 0).setFill(GBC.HORIZONTAL).setWeight(1, 1).setInsets(0, 0, 0, 5));
        this.add(customer, new GBC(1, 0).setFill(GBC.HORIZONTAL).setWeight(10, 0).setInsets(0, 0, 0, 5));
        this.add(lbFGPN, new GBC(2, 0).setFill(GBC.HORIZONTAL).setWeight(1, 0).setInsets(0, 0, 0, 5));
        this.add(FGPN, new GBC(3, 0).setFill(GBC.HORIZONTAL).setWeight(20, 0).setInsets(0, 0, 0, 5));
        this.add(btnSearch, new GBC(4, 0).setFill(GBC.HORIZONTAL).setInsets(0, 0, 0, 10).setIpad(80,3));

    }

    private void btnSearchActionPerformed(ActionEvent evt) {
        String CustomerValue = (String) getCustomer().getSelectedItem();
        String FGPNValue = (String) getFGPN().getSelectedItem();
        searchData(CustomerValue, FGPNValue);
    }

    public void  btnsearchclick(){
        String CustomerValue = (String) getCustomer().getSelectedItem();
        String FGPNValue = (String) getFGPN().getSelectedItem();
        searchData(CustomerValue, FGPNValue);
    }

    private void comboxChangeActionPerformed(ItemEvent evt) {
        String CustomerValue = (String) getCustomer().getSelectedItem();
        intiFGPN(CustomerValue);
    }
    private void fgpncomboxChangeActionPerformed(ItemEvent evt) {
        btnsearchclick();
    }
    public void initCustomer(){
        java.util.List<CustomerDto> list = new ArrayList<CustomerDto>();
        list = mainController.findAllCustomer();
        if (list != null) {
            customer.removeAllItems();
            for (CustomerDto customerDto : list) {
                customer.addItem(customerDto.getName());
            }
            ApplicationBeanUtils.getBean(BomManagePanel.class).getAddFGPN().setEnabled(true);
        }else {
            ApplicationBeanUtils.getBean(BomManagePanel.class).getAddFGPN().setEnabled(false);
        }
        EventBusUtils.post(new StatusBarChangeEvent(ResourceBundleUtils.getString(ActionMessages.INIT_CUSTOMER_DONE)));
    }

    public void intiFGPN(String CustomerValue){
        try {

            if (StringUtils.isBlank(CustomerValue)) {
                throw new ApplicationException(ExceptionMessages.EXCEPTION_SEARCH_PN_CUSTOMER_CANNOT_BE_EMPTY);
            }

            java.util.List<String> list = new ArrayList<String>();
            list = mainController.findAllFGPN(CustomerValue);
            FGPN.removeAllItems();

            if (list != null){
                for (String finishGood: list) {
                    FGPN.addItem(finishGood);
                }
            }
            btnsearchclick();
            EventBusUtils.post(new StatusBarChangeEvent(ResourceBundleUtils.getString(ActionMessages.INIT_FGPN_DONE)));

        }catch (ApplicationException ex){
            if(ResourceBundleUtils.getString(ex.getMessage()) == null || "".equals(ResourceBundleUtils.getString(ex.getMessage()))){
                EventBusUtils.post(new StatusBarChangeEvent(ResourceBundleUtils.getString(ExceptionMessages.EXCEPTION_GLOBAL_SYSTERM_ERROR), StatusBarChangeEvent.MSG_LEVEL_ERROR));
            }else
            EventBusUtils.post(new StatusBarChangeEvent(ResourceBundleUtils.getString(ex.getMessage()), StatusBarChangeEvent.MSG_LEVEL_ERROR));
        }

    }


    public void searchData(String CustomerValue, String FGPNValue) {

        try {
            if (StringUtils.isBlank(CustomerValue)) {
                throw new ApplicationException(ExceptionMessages.EXCEPTION_SEARCH_PN_CUSTOMER_CANNOT_BE_EMPTY);
            }
            if (StringUtils.isBlank(FGPNValue)) {
                throw new ApplicationException(ExceptionMessages.EXCEPTION_SEARCH_PN_FGPN_CANNOT_BE_EMPTY);
            }

            java.util.List<FinishGoodDto> list = new ArrayList<FinishGoodDto>();
            list = mainController.findFinishGoods(CustomerValue, FGPNValue);

            ApplicationBeanUtils.getBean(BomPNPanel.class).setTextFieldValues(list);
            ApplicationBeanUtils.getBean(BomPNPanel.class).initlastvale();
            EventBusUtils.post(new StatusBarChangeEvent(ResourceBundleUtils.getString(ActionMessages.SEARCH_DONE)));


        } catch (ApplicationException ex) {
            if(ResourceBundleUtils.getString(ex.getMessage()) == null || "".equals(ResourceBundleUtils.getString(ex.getMessage()))){
                EventBusUtils.post(new StatusBarChangeEvent(ResourceBundleUtils.getString(ExceptionMessages.EXCEPTION_GLOBAL_SYSTERM_ERROR), StatusBarChangeEvent.MSG_LEVEL_ERROR));
            }else
                EventBusUtils.post(new StatusBarChangeEvent(ResourceBundleUtils.getString(ex.getMessage()), StatusBarChangeEvent.MSG_LEVEL_ERROR));
        }
    }


    public JComboBox getCustomer() {
        return customer;
    }

    public void setCustomer(JComboBox customer) {
        this.customer = customer;
    }

    public JComboBox getFGPN() {
        return FGPN;
    }

    public void setFGPN(JComboBox FGPN) {
        this.FGPN = FGPN;
    }

    public JButton getBtnSearch() {
        return btnSearch;
    }

    public void setBtnSearch(JButton btnSearch) {
        this.btnSearch = btnSearch;
    }

    private JLabel lbcustomer;
    private JComboBox customer;
    private JLabel lbFGPN;
    private JComboBox FGPN;
    private JButton btnSearch;
}
